#!/bin/sh
INCLUDE_ONLY=1

. ../netifd-proto.sh
. ./ppp.sh

init_proto "$@"

proto_4g_init_config() {
	#return 
	no_device=1
	available=1
	ppp_generic_init_config
	proto_config_add_string "device:device"
	proto_config_add_string "apn"
	proto_config_add_string "service"
	proto_config_add_string "pincode"
	proto_config_add_string "dialnumber"
	#proto_config_add_string "type"
	
}

proto_4g_setup() {
	local interface="$1"
	local chat

	json_get_var device device
	json_get_var apn apn
	json_get_var service service
	json_get_var pincode pincode
	json_get_var dialnumber dialnumber
	#json_get_var type type
	


	[ -e "$device" ] || {
		proto_set_available "$interface" 0
		return 1
	}

	case "$service" in
		ct-lte)
			chat="/etc/chatscripts/ctlte.chat"
		;;
		cu-lte)
			chat="/etc/chatscripts/culte.chat"
		;;
		cm-lte)
			chat="/etc/chatscripts/cmlte.chat"
		;;
		*)
			echo "no chat for it" > /dev/console
		;;
	esac

	connect="/usr/sbin/chat -t5 -v -E -f $chat"
	ppp_generic_setup "$interface" \
		noaccomp \
		nopcomp \
		novj \
		nobsdcomp \
		noauth \
		lock \
		crtscts \
		115200 "$device"
	return 0
}

proto_4g_teardown() {
	proto_kill_command "$interface"
}

add_protocol 4g
